<?php
namespace app\admin\controller;
use app\admin\model\IndexTableModel;
use app\admin\model\JoinTableModel;
use think\Db;
use think\Request;
use app\common\model\User;
use app\common\model\UserLevel;
use app\common\validate\UserValidate;

require_once (__DIR__.'/../../../vendor/ReidsLock.class.php');
class FamemberlabelController extends Controller
{

    /**
     * 用户喜好
     * User:@hang
     */
    public function index(Request $request)
    {
        $param = $request->param();
        $whehe = array();
        if (isset($param['member_id']) && !empty($param['member_id'])) {
            $whehe[] = ['b.member_id','=',$param['member_id']];
        }
        if (isset($param['me_uid']) && !empty($param['me_uid'])) {
            $whehe[] = ['b.me_uid','like',"%{$param['me_uid']}%"];
        }
        if (isset($param['me_phone']) && !empty($param['me_phone'])) {
            $whehe[] = ['b.me_phone','like',"%{$param['me_phone']}%"];
        }
        if (isset($param['me_nick_name']) && !empty($param['me_nick_name'])) {
            $whehe[] = ['b.me_nick_name','like',"%{$param['me_nick_name']}%"];
        }
        $IndexTableModel = New IndexTableModel();
        if (isset($param['label_name']) && !empty($param['label_name'])) {
            $labelInfo = $IndexTableModel->getFind('fa_label',"`label_name` like '%{$param['label_name']}%'",'id');
            if(!empty($labelInfo)){
                $whehe[] = ['a.label_id','=',$labelInfo['id']];
            }
        }

        $data = Db::table('fa_member_label')
            ->alias('a')
            ->join("fa_member b",'a.member_id = b.member_id','LEFT')
            ->where($whehe)
            ->field("a.*,b.member_id as x_member_id,b.me_uid,b.me_phone,b.me_sex,b.me_nick_name,b.me_status")
            ->group("a.member_id")
            ->order('a.add_time desc')
            ->paginate($this->admin['per_page'],false,['query'=>request()->param()]);
        $data->each(function ($v, $k)use($IndexTableModel) {
            switch($v['me_sex']){
                case '0': $v['me_sex_word'] = '未设置';break;
                case '1': $v['me_sex_word'] = '男';break;
                case '2': $v['me_sex_word'] = '女';break;
            }
            if(!empty(strpos($v['me_phone'], 'zx'))){
                $v['me_phone'] = explode('zx',$v['me_phone'])[0].'（已注销）';
                $v['me_nick_name'] = explode('zx',$v['me_nick_name'])[0].'（已注销）';
            }
            $like_lable_list = Db::table('fa_member_label')
                ->alias('a')
                ->join("fa_label b",'a.label_id = b.id','LEFT')
                ->where("a.member_id = '{$v['member_id']}'")
                ->field('a.label_id,b.label_name')
                ->order("num desc")
                ->select();
            $like_str = "";
            foreach ($like_lable_list as $kk=>$vv){
                $like_str .= "【".$vv['label_name']."】".' / ';
            }
            $v['like_str'] = trim($like_str,' / ');
            return $v;
        });

        //关键词，排序等赋值
        $this->assign($request->get());
        $this->assign([
            'data'  => $data,
            'page'  => $data->render(),
            'total' => $data->total(),
        ]);
        return $this->fetch();
    }



}
